<template>
  <ele-modal
    form
    :width="1400"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        row-key="id"
        row-click-checked
      >
        <template #toolbar="{}">
          <el-button
            type="primary"
            v-if="props.action === 'buy' || props.action === 'wating-confirm'"
            @click="toDone()"
          >
            设为已出单
          </el-button>
        </template>
        <template #rebate="{ row }">
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.rebate"
          />
          <span v-else>{{ row.rebate }}</span>
        </template>
        <template #insurance_salesman="{ row }">
          <span>{{ row.insurance_salesman }}</span>
        </template>
        <template #insurance="{ row }">
          <el-select
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.insurance_id"
            placeholder="请选择报价的保险公司"
            filterable
            @change="handleInsuranceChange"
          >
            <el-option label="请选择" :value="0">请选择</el-option>
            <el-option
              v-for="(item, index) in insuranceList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              @click="setInsurance(index, row)"
            >
              {{ item.title }}
            </el-option>
          </el-select>
          <span v-else>{{ row.insurance }}</span>
        </template>
        <template #price="{ row }">
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.price"
          />
          <span v-else class="red">{{ row.price }}</span>
        </template>
        <template #s_time="{ row }">
          <el-date-picker
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.s_time_date"
            type="date"
            placeholder="结束日期"
            style="width: 120px"
            value-format="YYYY-MM-DD"
            :clearable="false"
          />
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model.number="row.s_time_h"
            class="ml3"
            style="width: 73px"
          >
            <template #append>时</template>
          </el-input>
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model.number="row.s_time_i"
            class="ml3"
            style="width: 73px"
          >
            <template #append>分</template>
          </el-input>
          <span v-else>{{ row.sTime }}</span>
        </template>
        <template #e_time="{ row }">
          <el-date-picker
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.e_time_date"
            type="date"
            placeholder="结束日期"
            style="width: 120px"
            value-format="YYYY-MM-DD"
            :clearable="false"
          />
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model.number="row.e_time_h"
            class="ml3"
            style="width: 73px"
          >
            <template #append>时</template>
          </el-input>
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model.number="row.e_time_i"
            class="ml3"
            style="width: 73px"
          >
            <template #append>分</template>
          </el-input>
          <span v-else>{{ row.eTime }}</span>
        </template>
        <template #pdf="{ row }">
          <file
            v-if="props.action === 'buy' || props.action === 'wating-confirm'"
            botton-text="上传"
            botton-size="small"
            botton-type="success"
            style="margin-top: 10px; margin-right: 8px"
            @done="onUpladDone($event, row)"
          />
          <span
            v-if="row.pdf !== ''"
            @click="openPdf(row.pdf)"
            class="blue pointer"
            >查看</span
          >
        </template>
        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #action="{ row }">
          <el-button type="primary" @click="openRefund(row)"> 退保 </el-button>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 图片 -->
    <images v-model="showImages" :data="current" :read-only="true" />
    <!-- 退保 -->
    <Refund v-model="showRefund" :data="current" />
  </ele-modal>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import {
    pageInsurance,
    pageInsuranceCompanyList,
    updateInsurancePdf
  } from '@/api/company';
  const emit = defineEmits(['done', 'update:modelValue']);

  import Images from './images.vue';
  import Refund from '@/views/insurance/refund/components/create.vue';
  import file from '@/components/FileUpload/file.vue';

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    data: Object,
    planId: {
      type: Number,
      default: 0
    },
    vehicleId: {
      type: Number,
      default: 0
    },

    offerType: {
      type: String,
      default: '全部'
    },

    action: String
  });
  const title = ref('');
  const current = ref([]);
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: '编号'
      },
      {
        width: 100,
        prop: 'cate',
        label: '类型'
      },
      {
        width: 100,
        prop: 'plate_number',
        label: '车牌号'
      },
      {
        slot: 'insurance',
        label: '保险公司'
      },
      {
        width: 100,
        slot: 'insurance_salesman',
        label: '业务员'
      },
      {
        width: 80,
        slot: 'rebate',
        label: '返点'
      },
      {
        width: 100,
        slot: 'price',
        label: '价格'
      },
      {
        width: 100,
        prop: 'raise_price',
        label: '加价'
      },
      {
        width: 200,
        slot: 's_time',
        label: '开始日期'
      },
      {
        width: 200,
        slot: 'e_time',
        label: '结束日期'
      },
      {
        width: 150,
        prop: 'createDate',
        label: '报价日期'
      },
      {
        width: 100,
        slot: 'img',
        label: '报价图片',
        align: 'center'
      }
    );
    if (
      props.action === 'buy' ||
      props.action === 'done' ||
      props.action === 'wating-confirm'
    ) {
      cols.push({
        label: '电子保单',
        slot: 'pdf',
        width: 120,
        align: 'center'
      });
    }
    if (props.action === 'done') {
      cols.push({
        label: '操作',
        slot: 'action',
        width: 120,
        align: 'center'
      });
    }
    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 表格数据源 */
  var offerList = ref({});
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsurance({
      ...where,
      ...orders,
      page,
      limit,
      plan_id: props.planId,
      cate: props.offerType,
      action: props.action
    });
    offerList.value = result.list;
    return result;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 退保 */
  const showRefund = ref(false);
  const openRefund = (row) => {
    current.value = row;
    showRefund.value = true;
  };

  /** 保险公司列表 */
  const insuranceList = ref([]);
  const getInsuranceList = () => {
    pageInsuranceCompanyList({ page: 1, limit: 99999999 })
      .then((res) => {
        insuranceList.value = res.list;
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  };

  const setInsurance = (i, row) => {
    row.insurance = insuranceList.value[i].title;
  };

  const handleInsuranceChange = () => {};

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 上传pdf完毕 */
  const onUpladDone = (e, row) => {
    console.log('上传成功', e, row);
    const requestData = {
      url: e,
      id: row.id
    };
    updateInsurancePdf(requestData)
      .then((res) => {
        console.log('更新成功', res);
        reload();
      })
      .catch(() => {});
  };
  const openPdf = (url) => {
    window.open(url);
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.data.plate_number + '的报价列表';
        if (props.action === 'buy') {
          title.value = props.data.plate_number + '的出单要求';
          getInsuranceList();
        }
        console.log(
          props.action,
          props.action == 'buy' || props.action == 'wating-confirm',
          '打开'
        );
      } else {
        console.log('关闭');
      }
    }
  );
</script>
